﻿using DataBase;
using DataBase.From;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication1.Controllers
{
    /// <summary>
    /// 华云测试转发接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HuaYunController : ControllerBase
    {
        public FreeSqlService FreeSqlService = new FreeSqlService();

        /// <summary>
        /// 获取最新的数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public DataBase.Test.TEarthNail GetLastData()
        {
            var res = FreeSqlService.Test
                .Queryable<DataBase.Test.TEarthNail>()
                .OrderByDescending(t => t.Id).First();
            return res;
        }

        /// <summary>
        /// 获取最新的几个数据
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        [HttpGet]
        public List<DataBase.Test.TEarthNail> GetDataByNum(int count)
        {
            var res = FreeSqlService.Test
                .Queryable<DataBase.Test.TEarthNail>()
                .OrderByDescending(t => t.Id).Take(count)
                .ToList();
            return res;
        }

        /// <summary>
        /// 获取时间范围内的数据
        /// </summary>
        /// <param name="startTime">开始时间，必填</param>
        /// <param name="endTime">结束时间，必填</param>
        /// <returns></returns>
        [HttpGet]
        public List<DataBase.Test.TEarthNail> GetDataByTime(DateTime startTime, DateTime endTime)
        {
            var res = FreeSqlService.Test
                .Queryable<DataBase.Test.TEarthNail>()
                .Where(t => t.CreateTime > startTime && t.CreateTime < endTime)
                .ToList();
            return res;
        }
    }
}
